package com.weimob.mcloud.wechatwork.customer.externalDependencyProxy.contenttouchradar;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.core.adapter.enums.cdp.CdpMarkMessageEventEnum;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.cdp.response.customer.CDPUserLogBO;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.saas.mcloud.wechatwork.content.touch.radar.api.domain.bo.RadarConfigDetailBO;
import com.weimob.saas.mcloud.wechatwork.content.touch.radar.api.domain.dto.RadarConfigIdsDTO;
import com.weimob.saas.mcloud.wechatwork.content.touch.radar.api.domain.dto.RadarGenerateURLDTO;
import com.weimob.saas.mcloud.wechatwork.content.touch.radar.api.enums.GenerateURLEnum;
import com.weimob.saas.mcloud.wechatwork.content.touch.radar.api.service.RadarExport;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Component
public class RadarProxy {
    @Reference
    private RadarExport radarExport;


    /**
     * 根据雷达ID批量查询雷达信息
     *
     * @return Map<String, RadarConfigDetailBO>
     */
    public Map<String, RadarConfigDetailBO> findRadarByIds(List<CDPUserLogBO> cdpUserLogBOList, String corpId,BasicInfo basicInfo) {
        Map<String, RadarConfigDetailBO> resultMap = new HashMap<>();

        try {
            if (!CollectionUtils.isEmpty(cdpUserLogBOList)) {
                Set<String> radarIds = cdpUserLogBOList.stream()
                        .filter((e) -> CdpMarkMessageEventEnum.CLICK_RADAR.getEventType().equals(e.getEventType()) || CdpMarkMessageEventEnum.EXIT_RADAR.getEventType().equals(e.getEventType()))
                        .map((e) -> e.getUserBehaviorDTO().getRadarId())
                        .collect(Collectors.toSet());

                if (!CollectionUtils.isEmpty(radarIds)) {
                    RadarConfigIdsDTO radarConfigIdsDTO = new RadarConfigIdsDTO();
                    radarConfigIdsDTO.setCorpId(corpId);
                    com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo commonBasicInfo = new com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo();
                    BeanUtils.copyProperties(basicInfo, commonBasicInfo);
                    radarConfigIdsDTO.setBasicInfo(commonBasicInfo);
                    radarConfigIdsDTO.setRadarIds(new ArrayList<>(radarIds));
                    radarConfigIdsDTO.setFetchDeleted(true);
                    List<RadarConfigDetailBO> radarConfigDetailBOList = SoaUtil.unpack(radarExport.findRadarByIds(radarConfigIdsDTO));
                    if (!CollectionUtils.isEmpty(radarConfigDetailBOList)) {
                        resultMap = radarConfigDetailBOList.stream().collect(Collectors.toMap(RadarConfigDetailBO::getRadarId, Function.identity(), (k1, k2) -> k1));
                    }
                }
            }
        } catch (Exception e) {
            log.error("根据雷达ID批量查询雷达信息 报错", e);
        }

        return resultMap;
    }

    /**
     * 生成雷达标题对应的URL
     * @param basicInfo
     * @return
     */
    public String generateURL(BasicInfo basicInfo){
        try {

            RadarGenerateURLDTO radarGenerateURLDTO = new RadarGenerateURLDTO();
            com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo basicInfoReq = new com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo();
            BeanUtils.copyProperties(basicInfo,basicInfoReq);
            radarGenerateURLDTO.setBasicInfo(basicInfoReq);
            radarGenerateURLDTO.setGenerateURLType(GenerateURLEnum.BASH_BOARD.getUrlType());
            return SoaUtil.unpack(radarExport.generateURL(radarGenerateURLDTO));

        } catch (Exception e) {
            log.error("生成雷达标题对应的URL报错", e);
            return null;
        }
    }

}
